home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Audio / Patchmix / Source / Oscil.m < prev    next >
Text File  |  1992-08-09  |  6KB  |  253 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "Oscil.h"
  5. #import "Instrum.h"
  6. #import <appkit/graphics.h>
  7. #import "Statement.h"
  8.  
  9. @implementation Oscil
  10.  
  11. + initialize
  12. {
  13.        image = [NXImage findImageNamed:"Oscil"];
  14.     ampOffset.x = 17;
  15.     ampOffset.y = 56;
  16.     freqOffset.x = 38;
  17.     freqOffset.y = 56;
  18.     phaseOffset.x = 57;
  19.     phaseOffset.y = 46;
  20.     outOffset.x = 27;
  21.     outOffset.y = 11;
  22.     
  23.     return self;
  24. }
  25.  
  26. - init
  27. {
  28.     written = NO;
  29.     size.width = 80.;
  30.     size.height = 80.;
  31.     center.x = size.width/2.;
  32.     center.y = size.height/2.;
  33.     strcpy(type,"Oscil");
  34.     strcpy(name,"oscili");
  35.     
  36.     amp = [[Param alloc] init:self :&Offset];
  37.     freq = [[Param alloc] init:self :&freqOffset];
  38.     phase = [[Param alloc] init:self :&phaseOffset];
  39.     out = [[Param alloc] init:self :&outOffset];
  40.  
  41.     [freq setValue:"440"];    // set non-"1" default values
  42.     [phase setValue:"0"];    // set non-"1" default values
  43.     [amp setTitle:"Amp:"];
  44.     [freq setTitle:"Freq:"];
  45.     [phase setTitle:"Phase:"];
  46.     [out setTitle:"Out:"];
  47.     
  48.     paramList = [[List alloc] initCount:4];
  49.     [paramList addObject:amp];
  50.     [paramList addObject:freq];
  51.     [paramList addObject:phase];
  52.     [paramList addObject:out];
  53.     
  54.     [Inst putUgenInList:self];
  55.     
  56.     return self;
  57. }
  58.  
  59. - remove
  60. {
  61.     id cp,param;
  62.     int i;
  63.                             // make sure not connected first 
  64.     for(i = 0; i < ([paramList count]); i++) {    
  65.         param = [paramList objectAt:i];
  66.         if(cp = [param getConnectedParam]) 
  67.             return 0;
  68.     }
  69.                             // remove ugen from list
  70.     [Inst removeUgenFromList:self];
  71.     return self;
  72. }
  73.  
  74. - (NXImage *)getImage
  75. {
  76.     return image;
  77. }
  78.  
  79. - move:(NXPoint *)newloc
  80. {
  81.     location = *newloc;
  82.     [amp move:&location];
  83.     [freq move:&location];
  84.     [phase move:&location];
  85.     [out move:&location];
  86.     return self;
  87. }
  88.  
  89. - findParamAtPoint:(NXPoint *)point
  90. {
  91.     NXRect *rect;
  92.     int i;
  93.     id param;
  94.     
  95.     for(i = 0; i < [paramList count]; i++) {
  96.         param = [paramList objectAt:i];
  97.         rect = [param getRect];
  98.         if(NXMouseInRect(point,rect,NO))
  99.             return param;
  100.     }
  101.     //printf("no param found\n");
  102.     return nil;
  103. }
  104.  
  105. - writeUgen
  106. {
  107.     BOOL am = NO;
  108.     BOOL fm = NO;
  109.     BOOL rm = NO;
  110.     BOOL pi = NO;
  111.     char ampVar[10];
  112.     char freqVar[10];
  113.     /*     for each input param:
  114.             see if there is input ugen
  115.                 if so, grab the output (ug?) and multiply it by this
  116.                     param value
  117.                 if not just use the param value
  118.          after all params done, write relevant code into lists
  119.             (declarations, assignments and loop statements)
  120.             i.e. ug? = oscil(,,,,);
  121.         go to output param connected ugen and call its "writeUgen"
  122.     */
  123.     int i;
  124.     int parent[3];
  125.     id param;
  126.     id ug;
  127.     id nupar;
  128.     char str[50];
  129.     char par[20];
  130.     
  131.     //printf("writing ugen %d\n",index);
  132.     sprintf(str,"\tfloat ug%d;\n",index);
  133.     [Inst putVarInList:str];
  134.     sprintf(str,"\tint len%d;\n",index);
  135.     [Inst putVarInList:str];
  136.     sprintf(str,"\tint wave%d;\n",index);
  137.     [Inst putVarInList:str];
  138.     sprintf(str,"\tfloat amp%d;\n",index);
  139.     [Inst putVarInList:str];
  140.     sprintf(str,"\tfloat si%d;\n",index);
  141.     [Inst putVarInList:str];
  142.     sprintf(str,"\tfloat phase%d;\n",index);
  143.     [Inst putVarInList:str];
  144.     sprintf(str,"\tfloat out%d;\n",index);
  145.     [Inst putVarInList:str];
  146.     sprintf(str,"\tfloat *f%d;\n",index);
  147.     [Inst putVarInList:str];
  148.  
  149.     for(i = 0; i < ([paramList count]-1); i++) {    
  150.         param = [paramList objectAt:i];
  151.         if(nupar = [param getConnectedParam]) {
  152.             ug = [nupar getUgen];
  153.             if([ug getWritten] == NO) { 
  154.                 parent[i] = [[ug writeUgen] getIndex];
  155.             }
  156.             else  {
  157.                 parent[i] = 0;
  158.             }
  159.         }
  160.         else 
  161.             parent[i] = 0;
  162.         
  163.     }
  164.     sprintf(str,"\twave%d = 1;\n",index); // wave
  165.     [Inst putAssignInList:str];
  166.     
  167.     if(parent[0]) {     // amplitude input ugen    
  168.         //printf("am or ring mod\n");
  169.         rm = YES;
  170.         sprintf(str,"\tamp%d = %.2f;\n",index, 
  171.             atof([[paramList objectAt:0] getValue]));
  172.         [Inst putAssignInList:str];
  173.         sprintf(str,"\tfloat amprm%d;\n",index);
  174.         [Inst putVarInList:str];
  175.         sprintf(str,"\t\tamprm%d = amp%d * ug%d;\n",index,  
  176.             index,parent[0]);
  177.         [Inst putLoopInList:str];
  178.     }
  179.     else {                // amp preset or default
  180.         //printf("amp preset or pfield\n");
  181.         strcpy(par,[[paramList objectAt:0] getValue]);
  182.         if(par[0] == 'p' && par[1] == '[')
  183.             sprintf(str,"\tamp%d = %s;\n", index, par);
  184.         else
  185.             sprintf(str,"\tamp%d = %.2f;\n",index,atof(par));
  186.         [Inst putAssignInList:str];
  187.     }
  188.     if(parent[1]) {
  189.         if(!strcmp([[[freq getConnectedParam] getUgen] getType],"Converter")) { 
  190.             strcpy(par,[[paramList objectAt:1] getValue]);
  191.             sprintf(str,"\tsi%d = ug%d*%.2f*fsize(1)/SR;\n", index, parent[1], atof(par));
  192.         }
  193.         else {
  194.                             // fm selected
  195.             strcpy(name,"oscilni");        //printf("fm\n");
  196.             fm = YES;
  197.             sprintf(str,"\tfloat sifm%d;\n",index);
  198.             [Inst putVarInList:str];
  199.             sprintf(str,"\t\tsifm%d = si%d + ug%d * len%d/SR;\n", index,index,parent[1],index);
  200.             [Inst putLoopInList:str];
  201.             sprintf(str,"\tsi%d = %.2f*fsize(1)/SR;\n",index, 
  202.                 atof([[paramList objectAt:1] getValue]),index);
  203.         }
  204.         [Inst putAssignInList:str];
  205.     }
  206.     else {                 // freq preset
  207.         //printf("freq preset or pfield\n");
  208.         strcpy(par,[[paramList objectAt:1] getValue]);
  209.         if(par[0] == 'p' && par[1] == '[')
  210.             sprintf(str,"\tsi%d = %s*fsize(1)/SR;\n", index, par);
  211.         else
  212.             sprintf(str,"\tsi%d = %.2f*fsize(1)/SR;\n", index, atof(par));
  213.         [Inst putAssignInList:str];
  214.     }
  215.     
  216.     strcpy(par,[[paramList objectAt:2] getValue]);
  217.     if(par[0] == 'p' && par[1] == '[')
  218.         sprintf(str,"\tphase%d = %s;\n",index,par);
  219.     else
  220.         sprintf(str,"\tphase%d = %.2f;\n", index, atof([[paramList objectAt:1] getValue]));
  221.     
  222.     [Inst putAssignInList:str];
  223.     sprintf(str,"\tlen%d = fsize(wave%d);\n",index,index);
  224.     [Inst putAssignInList:str];
  225.     sprintf(str,"\tf%d = (float *)floc(wave%d);\n",index,index);
  226.     [Inst putAssignInList:str];
  227.     
  228.     if(rm)
  229.         sprintf(ampVar,"amprm%d",index);
  230.     else
  231.         sprintf(ampVar,"amp%d",index);
  232.     if(fm)
  233.         sprintf(freqVar,"sifm%d",index);
  234.     else
  235.         sprintf(freqVar,"si%d",index);
  236.         
  237.                     // output amplitude multiplier:
  238.     strcpy(par,[[paramList objectAt:3] getValue]);
  239.     if(par[0] == 'p' && par[1] == '[')
  240.         sprintf(str,"\tout%d = %s;\n",index,par);
  241.     else
  242.         sprintf(str,"\tout%d = %.2f;\n", index, atof([[paramList objectAt:3] getValue]));
  243.     [Inst putAssignInList:str];
  244.  
  245.     sprintf(str,"\t\tug%d = %s(%s,%s,f%d,len%d,&phase%d)*out%d;\n", index,name,ampVar, freqVar,index,index,index,index);
  246.     
  247.     [Inst putLoopInList:str];
  248.     written = YES;
  249.     return self;    
  250. }
  251.  
  252. @end
  253.